package com.example.module_mine.repository

import com.example.lib_storage.utils.GSON
import com.example.module_mine.MineModel
import com.example.module_mine.api.MineApi
import com.example.module_mine.model.LoginMode
import com.example.module_mine.model.LoginPostModel
import kotlinx.coroutines.flow.flow
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody
import javax.inject.Inject

class MineRepository @Inject constructor() {
    @Inject
    @MineModel.SignApi
    lateinit var mineApi: MineApi
    fun sendCodeLogin(operatorType:String,phone:String) = flow{
        emit(mineApi.sendLoginCode(operatorType,phone))
    }
    fun codeLogin(
        grantType: LoginMode,
        newPassword:String="",
        operatorType:Int=2,
        password:String="",
        smsCode:Int,
        userName:String
    ) = flow{
        val postModel = LoginPostModel(grantType.value,newPassword,operatorType,password,smsCode,userName)
        val body = RequestBody.create("application/json;charset=utf-8".toMediaType(), GSON.toJson(postModel))
        emit(mineApi.codeLogin(body))
    }
}